package cn.lee.cplibrary.widget.recycler.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import cn.lee.cplibrary.init.CPInitializer;


/**
 * RecyclerView网格布局实现间距（GridLayoutManager）
 * 有一个头布局其position==0
 * Create by ChrisLee on 2021-09-14
 */
public class GridHeaderDecoration extends RecyclerView.ItemDecoration {
    private int marginCol; //第1列距左边、最后一列距右边的距离
    private int itemSpacingCol; //item的列间隔（去掉首尾列）


    public GridHeaderDecoration(int marginCol, int itemSpacing) {
        CPInitializer.checkInitialization();
        this.marginCol = marginCol;
        this.itemSpacingCol = itemSpacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int spanCount = getSpanCount(parent);
        int position = parent.getChildAdapterPosition(view); //item position
//        LogUtil.i("", "spanCount=" + spanCount);
        //int column = position % spanCount; // item column
        //此处为头布局
        if (position == 0) {
            outRect.left = 0;
            outRect.right = 0;
            outRect.top = 0;
            outRect.bottom = 0;
        } else {
            //列间距
            if (isFirstColumn(parent, position, spanCount)) {
//                LogUtil.i("", "isFirstColumn=" + position);
                outRect.left = marginCol;
                outRect.right = itemSpacingCol / 2;
            } else if (isLastColumn(parent, position, spanCount)) {//最后一列
                outRect.left = itemSpacingCol / 2;
                outRect.right = marginCol;
//                LogUtil.i("", "isLastColumn=" + position);
            } else {
                outRect.left = itemSpacingCol / 2;
                outRect.right = itemSpacingCol / 2;
//                LogUtil.i("", "中间=" + position);
            }
        }

    }

    /**
     * 判读是否是第一列
     *
     * @param parent
     * @param pos
     * @param spanCount
     * @return 是否是第一列
     */
    private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if (pos % spanCount == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是最后一列
     *
     * @param parent
     * @param pos
     * @param spanCount 3
     * @return
     */
    private boolean isLastColumn(RecyclerView parent, int pos, int spanCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 1) {// 如果是最后一列，则不需要绘制右边
                return true;
            }
        }
        return false;
    }


    /**
     * 获取列数
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }


    public void setMarginCol(int marginCol) {
        this.marginCol = marginCol;
    }

    public void setItemSpacingCol(int itemSpacingCol) {
        this.itemSpacingCol = itemSpacingCol;
    }

}
